﻿using BuildDatabase.Factory;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace BuildDatabase
{
    class Program
    {
        static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();

#if Release
            UpdateSchema();
#else
            CreateDatabase();
            BuildSchema();
            TellStory();
#endif

            Console.WriteLine("finished!");
            Console.Read();

        }

        private static void TellStory()
        {
            ISession session = NHProvider.session;
            using (var tran = session.BeginTransaction())
            {
                RegisterFactory.Create();
                PlanFactory.Create();
                ProfileFactory.Create();
                KeywordFactory.Create();
                TaskFactory.Create();
                MessageFactory.Create();

                try
                {
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }

            }
        }

        static NHibernate.Cfg.Configuration config = NHProvider.getConfig().BuildConfiguration();

        static void BuildSchema()
        {
            var schema = new SchemaExport(config);
            schema.Create(true, true);    //TODO: don't know why the two parameter must be true?
        }

        static void UpdateSchema()
        {
            string path = Environment.CurrentDirectory.Substring(0, Environment.CurrentDirectory.IndexOf("bin")) + "Update\\sqlscript.txt";
            using (var file = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
            {
                using (var sw = new StreamWriter(file))
                {
                    new SchemaUpdate(config)
                        .Execute(sw.Write, false);
                }
            };
        }

        static void CreateDatabase()
        {
            Global.NHibernateTestHelper.BuildDatabase.CreateDatabase("ducu_dev");
        }
    }
}
